package com.android.lovingpet.adopt.adoption.adopt.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.adopt.adoption.adopt.AdoptPetPresenter;
import com.android.lovingpet.adopt.adoption.adopt.IAdoptPetContract;
import com.android.lovingpet.adopt.chat.ChatActivity;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.bean.Team;
import com.bumptech.glide.Glide;

import java.time.LocalDate;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.android.lovingpet.adopt.adoption.adopt.AdoptPetPresenter.FLAG_PUBLISHER;


//发布人 需要从pet的masterId获取发布人信息


public class PetDetailsActivity extends BaseActivity<IAdoptPetContract.View,AdoptPetPresenter> implements View.OnClickListener,IAdoptPetContract.View{

    @BindView(R.id.iv_video)
    ImageView ivVideo;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_source)
    TextView tvSource;
    @BindView(R.id.tv_description)
    TextView tvDescription;
    @BindView(R.id.tv_requirements)
    TextView tvRequirements;
    @BindView(R.id.tv_publisher)
    TextView tvPublisher;
    @BindView(R.id.tv_sterilize)
    TextView tvSterilize;
    @BindView(R.id.bt_adopt)
    Button btAdopt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_details);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    public AdoptPetPresenter createPresenter() {
        return new AdoptPetPresenter();
    }

    @Override
    public IAdoptPetContract.View createView() {
        return this;
    }

    public void initView()
    {
        //设置标题
        TextView tvTitle= findViewById(R.id.tv_title);
        tvTitle.setText("宠物详情");

        //接受上一页面传ni来的pet数据
        Pet pet=getIntent().getParcelableExtra("data");

        //获取发布人信息
        getPresenter().getMasterInfo(Integer.parseInt(pet.getMasterId()));

        tvName.setText(pet.getName());
        tvType.setText(pet.getType());
        if (pet.getSource()==1) {
            tvSource.setText("家养");
        }else{
            tvSource.setText("流浪");
        }
        tvDescription.setText(pet.getDescription());
        tvRequirements.setText(pet.getDemand());
        StringBuilder stringBuffer=new StringBuilder();
        if (pet.getSterilize()==1)
        {
            stringBuffer.append("已");
        }
        else {
            stringBuffer.append("未");
        }
        stringBuffer.append("绝育");
        if (pet.getImmune()==0)
        {
            stringBuffer.append(",未免疫");
        }
        else if (pet.getImmune()==1)
        {
            stringBuffer.append(",已免疫");
        }else
        {
            stringBuffer.append(",免疫不详");
        }
        tvSterilize.setText(stringBuffer.toString());
        Glide.with(PetDetailsActivity.this).load(pet.getImage()).into(ivVideo);
    }

    @OnClick({R.id.bt_adopt})
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.bt_adopt:
                if (team != null) {
                    Intent intent = new Intent(PetDetailsActivity.this, ChatActivity.class);
                    intent.putExtra("team", team);
                    startActivity(intent);
                }
                //startActivity(new Intent(this,AdoptPetActivity.class));
                break;
        }
    }

    @Override
    public void next(Bundle bundle) {
        int flag=bundle.getInt("flag");
        String data=bundle.getString("data");
        if (flag==FLAG_PUBLISHER) {
            runOnUiThread(() -> tvPublisher.setText(data));
        }else {
            runOnUiThread(()->tvCity.setText(data));
        }
    }

    @Override
    public void setImage(String url) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!PetDetailsActivity.this.isDestroyed()) {
                    Glide.with(PetDetailsActivity.this).load(url).into(ivVideo);
                }
            }
        });
    }

    private Team team;
    @Override
    public void setTeam(Team team) {
        this.team = team;
    }
}
